package com.example.json;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonTest {

	public static void main(String[] args) throws Exception {
		test1();
	}

	public static User test1() throws FileNotFoundException, IOException, ClassNotFoundException {
		User u = User.getBuilder("小明", 23).love("play lol").appearance("handsome").build();
		
		User u2 = User.getBuilder("小明的儿子", 12).love("drink coca cola").appearance("handsome").build();
		User u3 = User.getBuilder("小明的儿子的儿子", 5).love("drink baishi cola").appearance("handsome").build();
		User u4 = User.getBuilder("小明的儿子的儿子的儿子", 1).love("hahaha").appearance("handsome").build();
		
		u.addChild(u2);
		
		u2.addChild(u3);
		u2.setParent(u);
		
		u3.addChild(u4);
		u3.setParent(u2);
		
		ObjectMapper mapper = new ObjectMapper();

		String json = mapper.writeValueAsString(u);
		System.out.println(json);

		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/user01.json"))) {
			oos.writeObject(u);
			oos.flush();
		}
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/user01.json"))) {
			User user = (User) ois.readObject();
			System.out.println("外貌：" + user.getAppearance());
			System.out.println("喜好：" + user.getLove());
		}
		return u;
	}

}
